<template>
  <div>
    <el-dialog :title="isEdit ? '创建用户':'编辑用户'"
               :visible.sync="AddUserDialogVisible"
               width="50%"
               :before-close="handleClose">
      <el-form ref="refUserform"
               :model="addUserform"
               :rules="subinfofromRuls"
               label-width="130px">
        <el-form-item label="企业名称"
                      prop="shortName">
          <el-input v-model="addUserform.shortName"></el-input>
        </el-form-item>

        <el-form-item label="所属公司"
                      prop="company">
          <el-input v-model="addUserform.company"></el-input>
        </el-form-item>

        <el-form-item>
          <el-link>https://www.tianyancha.com/</el-link>(在此可查询所属公司全称及简介)
        </el-form-item>

        <el-form-item label="城市地区"
                      prop="province">
          <el-select @change="onLoadCityList"
                     v-model="addUserform.province"
                     clearable
                     placeholder="请选择">
            <el-option v-for="(item,index) in provincelist"
                       :key="index"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
          <el-select v-model="addUserform.city"
                     clearable
                     placeholder="请选择">
            <el-option v-for="(item,index) in cityarea"
                       :key="index"
                       :label="item"
                       :value="item">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="方向(企业标签)"
                      prop="tags">
          <el-input v-model="addUserform.tags"></el-input>
        </el-form-item>

        <el-form-item label="备注"
                      prop="remarks">
          <el-input v-model="addUserform.remarks"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="handleClose()">取 消</el-button>
        <el-button type="primary"
                   @click="addEnterInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { provinces, citys } from '@/api/hmmm/citys.js'
import { add, update } from '@/api/hmmm/companys'
export default {
  name: 'AddComUser',
  props: {

  },
  data () {
    return {
      addUserform: {
        shortName: '',
        company: '',
        isFamous: true,
        province: '',
        city: '',
        tags: '',
        remarks: ''
      },
      isEdit: true,
      provincelist: [],
      cityarea: [],
      editInfo: {},
      restfrom: {},
      AddUserDialogVisible: false,
      subinfofromRuls: {
        shortName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        company: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        tags: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        remarks: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.provincelist = provinces()
  },
  methods: {
    onLoadCityList () {
      this.cityarea = citys(this.addUserform.province)
    },
    handleClose () {
      this.addUserform = this.restfrom
      this.AddUserDialogVisible = false
    },
    addEnterInfo () {
      this.$refs.refUserform.validate(async valid => {
        console.log(this.subinfofromRuls)
        if (!valid) {
          return null
        }
        if (this.isEdit === true) {
          try {
            await add(this.addUserform)
            this.$message.success('添加成功')
            this.$emit('LoadEnterInfo')
            this.handleClose()
          } catch (err) {
            this.$message.error('添加失败')
          }
        } else {
          this.subEditInfo()
        }
      })
    },
    editEnter () {
      this.AddUserDialogVisible = true
      this.isEdit = false
      this.addUserform = this.editInfo
      if (this.addUserform.isFamous === 1) {
        this.addUserform.isFamous = true
      } else if (this.addUserform.isFamous === 0) {
        this.addUserform.isFamous = false
      }
      console.log(this.editInfo)
    },
    async subEditInfo () {
      try {
        await update(this.addUserform)
        this.$message.success('修改成功')
        this.$emit('LoadEnterInfo')
        this.handleClose()
      } catch (err) {
        this.$message.error('修改失败')
      }
    }
  }
}
</script>

<style>
</style>
